'''
* This is the projet for Brtc LlmOps Platform
* @Author Leon-liao <liaosiliang@alltman.com>
* @Description //TODO 
* @File: password.py
* @Time: 2025/10/10
* @All Rights Reserve By Brtc
'''
import base64
import binascii
import hashlib
import re
from typing import Any

# 密码校验正则，密码最少包含一个字母、一个数字、并且长度为8-16
password_pattern = r"^(?=.*[a-zA-Z])(?=.*\d).{8,16}$"

def validate_password(password:str, pattern:str=password_pattern):
    """校验传入的密码是否符合相应的匹配规则"""
    if re.match(pattern, password) is None:
        raise ValueError("密码校验失败, 至少包含一个字母, 一个数字， 并且长度为8-16个字符")
    return

def hash_password(password:str, salt:Any)->bytes:
    """将传入的密码 + 盐值 进行hash 加密"""
    dk = hashlib.pbkdf2_hmac("sha256", password.encode("utf-8"), salt, 10000)
    return binascii.hexlify(dk)


def compare_password(password:str, password_hashed_base64:Any, salt_base64:Any)->bool:
    """根据传入的密码 + 盐值 对比密码是否一致"""
    print("====>", hash_password(password, base64.b64decode(salt_base64)))
    print("====>", base64.b64decode(password_hashed_base64))


    return hash_password(password, base64.b64decode(salt_base64)) == base64.b64decode(password_hashed_base64)


